package com.example.demo.dao;

import com.example.demo.model.Articleinfo;

import org.apache.ibatis.annotations.*;

import java.util.List;

public interface ArticleMapper {
    /**
     * 获取用户的文章列表
     * @param uid
     * @return
     */
    @Select("select * from articleinfo where uid=#{uid} order by id desc")
    List<Articleinfo> getListByUid(@Param("uid")int uid);

    /**
     * 根据文章 id 删除文章
     * @param aid
     * @param uid
     * @return
     */
    @Delete("delete from articleinfo where id=#{aid} and uid=#{uid}")
    int del(@Param("aid")Integer aid, int uid);

    /**
     * 添加文章
     * @param articleinfo
     * @return
     */
    @Insert("insert into articleinfo(title,content,uid) values(#{title},#{content},#{uid})")
    int add(Articleinfo articleinfo);

    /**
     * 修改之前，得先查询这篇文章
     * @param aid
     * @param uid
     * @return
     */
    @Select("select * from articleinfo where id=#{aid} and uid=#{uid}")
    Articleinfo getArticleByIdAndUid(@Param("aid") int aid,
                                     @Param("uid") int uid);

    /**
     * 修改操作
     * @param articleinfo
     * @return
     */
    @Update("update articleinfo set title=#{title},content=#{content} where id=#{id} and uid=#{uid}")
    int update(Articleinfo articleinfo);

    /**
     * 文章详情页面
     * @param aid
     * @return
     */
    @Select("select * from articleinfo where id=#{aid}")
    Articleinfo getDetailById(@Param("aid") int aid);

    /**
     * 通过用户的 id 来查询总文章数
     * @param uid
     * @return
     */
    @Select("select count(*) from articleinfo where uid=#{uid}")
    int getArtCountByUid(@Param("uid")int uid);

    /**
     * 文章的访问量
     * @param aid
     * @return
     */
    @Update("update articleinfo set rcount=rcount+1 where id=#{aid}")
    int incrementRCount(@Param("aid")int aid);

    /**
     * 分页管理
     * @param psize
     * @param offset
     * @return
     */
    @Select("select * from articleinfo order by id desc limit #{psize} offset #{offset}")
    public List<Articleinfo> getListByPage(@Param("psize")int psize,@Param("offset")int offset);

    /**
     * 获取文章总数
     * @return
     */
    @Select("select count(*) from articleinfo")
    int getCount();
}
